package com.wva.video.controller;

import com.wva.common.annotation.authority.Open;
import com.wva.common.enums.ObjectName;
import com.wva.common.exception.BusinessException;
import com.wva.common.utils.R;
import com.wva.video.constant.ResourceConst;
import com.wva.video.domain.vo.VideoResourceVO;
import com.wva.video.service.VideoResourceService;
import com.wva.video.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @Author: hong
 * @Date: 2025/2/16 00:00:24
 */
@Api(tags = "视频资源相关接口")
@RequestMapping("/video/resource")
@RestController
public class ResourceController {
    @Autowired
    VideoResourceService videoResourceService;
    @Autowired
    VideoService videoService;

    @ApiOperation("获取视频资源")
    @Open
    @GetMapping("/{videoId}")
    R<VideoResourceVO> get(@PathVariable Long videoId, @RequestParam String quality) throws Exception {
        if (videoService.getPublicWorkById(videoId) == null)
            throw new BusinessException("视频不存在");

        //生成链接
        String url = videoResourceService.getVideoObjectAccessUrlByMd5(
                ObjectName.UPLOAD.format(videoId, quality),
                ResourceConst.TEMP_URL_TIMEOUT,
                TimeUnit.SECONDS
        );

        return R.ok(
                VideoResourceVO.builder()
                        .temporaryPlayUrl(url)
                        .build()
        );
    }
}
